<?php

namespace App\Providers;


use Illuminate\Foundation\Http\Events\RequestHandled;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        if (config('app.env') === 'local') {
            DB::connection()->enableQueryLog();

            Event::listen(RequestHandled::class, function ($event) {
                if ($event->request->input('sql_debug')) {

                    $queries = DB::getQueryLog();

                    if (!empty($queries)) {
                        foreach ($queries as &$query) {
                            $query['full_query'] = vsprintf(str_replace('?', '%s', $query['query']), $query['bindings']);
                        }
                    }

                    dd($queries);
                }
            });

        }

    }
}
